package com.example.cloudmaps;

import java.util.ArrayList;
import java.util.Collections;

import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class FavoritePlaces extends Activity {
	
	private static final String PHP_FAV = "http://softeng.cs.uwosh.edu/students/meyers11/PHPGetFavPlaces.php";
	private String username;
	ArrayList<String> placeList = new ArrayList<String>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_favorite_places);
		Intent intent = getIntent();
		username = intent.getExtras().getString("username");
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.favorite_places, menu);
		return true;
	}

	
	public class GetFriendsTask extends AsyncTask<Void, Void, ArrayList<String>> {
		@Override
		protected ArrayList<String> doInBackground(Void... params) {
			// TODO: attempt authentication against a network service.
			String response = "";
			ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
			
			ArrayList<String> places = new ArrayList<String>(); 
			
			if(networkInfo != null && networkInfo.isConnected())
			{
				ArrayList<NameValuePair> n = new ArrayList<NameValuePair>(1);
				n.add(new BasicNameValuePair("username", username));
				
				HttpClient httpClient = new DefaultHttpClient();
				HttpPost httppost = new HttpPost(PHP_FAV);
				try
				{
					httppost.setEntity(new UrlEncodedFormEntity(n));
					ResponseHandler<String> respondHandler = new BasicResponseHandler();
					response = httpClient.execute(httppost, respondHandler);
					
					try{

						JSONArray jArray = new JSONArray(response);
						
						JSONObject json_data = null;

						for(int i = 0; i < jArray.length(); i++)
						{
							json_data = jArray.getJSONObject(i);
							
							String placeName = "";
							
							placeName = json_data.getString("name");

							places.add(placeName);
						}
						
					}catch(JSONException e1){
						e1.printStackTrace();
					}
				}
				catch(Exception e){
					
				}
			}
			
			return places;
		}
		
		@Override
		protected void onPostExecute(ArrayList<String> resp){
			
			Collections.sort(resp);
			placeList = resp;
			//display friends
			ListView lv;
			lv=(ListView)findViewById(R.id.favPlaceList);
			lv.setAdapter(new ArrayAdapter<String>(FavoritePlaces.this,android.R.layout.simple_list_item_1 , resp));
		}
		
	}

	


}
